#include "player.h"

#include <stddef>				// NULL
#include <algorithm>			// min


player::player(i):id(i) {
	exp = 0;
	point = 0;
	hand = NULL;
	
}


player::~player() {}


// ----------------------------------------


void player::setExp(int newValue) {
	exp = newValue;
	
	return;
	
}

void player::setPoint(int newValue) {
	point = newValue;
	
	return;
	
}

void player::setHand(const maajanHand &newValue) {
	hand = newValue;
	
	return;

}
int player::getExp() {
	return exp;
	
}

int player::getPoint() {
	return point;
	
}


// ----------------------------------------


void player::drawNewCard(const maajanTile &newValue) {
	hand[hand.getQtyHand()] = newValue;
	
	return;
	
}

maajanTile *player::discardRequest(int index) {
	return = (*hand)[index];
	
}

maajanTile *player::chiRequest(int index) {
	return (*hand)[index];
	
}


// ---------------------------------------------------------


int player::chiRequest(const maajanTile &currTile) {
	// *** NOT IMPLEMENTED YET *** //
	int result = 0;
	
	bool left_1 = false;
	bool left_2 = false;
	bool left_3 = false;
	bool right_1 = false;
	bool right_2 = false;
	bool right_3 = false;
	
	bool flag = false;
	int count = 0;
	int valCount = 0;
	
	int currVal = currTile.getValue();
	int currSet = currTile.getSet();
	int searchEndVal = min(9, currVal);
	
	
	for (count < TILES_NUM_HAND && flag == false; count++) {
		if ((*hand)[count].getSet == currSet) {
			flag = true;
		}
	}
	
	count--;
	
	if (flag == true) {
		valCount = (*hand)[count].getValue();
		for (count < TILES_NUM_HAND && (valCount <= searchEndVal) {
			if ((*hand)[count].getSet != currSet) { // no more possible counts
				break;
			}
			
			valCount = (*hand)[count].getValue();
			
			if (valCount == currVal - 2) {
				left_2 = true;
			} else if (valCount == currVal - 1) {
				left_1 = true;
			} else if (valCount == currVal + 1) {
				right_1 = true;
			} else if (valCount == currVal + 2) {
				right_2 = true;
			}
		}
	
		if ((left_1 && left_2) || (right_1 && right_2)) {
			// check for margins
			for (int i = 0; i < TILES_NUM_HAND; i++) {
				if ((*hand)[i].getSet() == currSet) {
					countVal = (*hand)[i].getValue();
					if (countVal == currVal - 3) {
						left_3 = true;
					}else if (countVal == currVal + 3) {
						right_3 = true;
					}
				}
			}
			
			
}


int player::ponRequest(const maajanTile &currTile) {
	int count = 0;
	
	int currSet = currTile.getSet();
	int currValue = currTile.getValue();
	
	for (int i = 0; i < TILES_NUM_HAND; i++) {
		if ((*hand)[i].getSet() == currSet &&
			(*hand)[i].getValue() == currValue) {
			count++;
		}
	}
	
	if (count == 2) {
		return 1;
	}
	else {
		return 0;
	}
}

int player::kanRequest(const maajanTile &currTile) {
	int count = 0;
	
	int currSet = currTile.getSet();
	int currValue = currTile.getValue();
	
	for (int i = 0; i < TILES_NUM_HAND; i++) {
		if ((*hand)[i].getSet() == currSet &&
			(*hand)[i].getValue() == currValue) {
			count++;
		}
	}
	
	if (count == 3) {
		return 1;
	}
	else {
		return 0;
	}
}

int ronRequest(const maajanTile &currTile) {
	


// ---------------------------------------------------------


void player::chi(	const int &index1),
					const int &index2),
					const maajanTile &newValue) {
					
	int listLength = hand.getQtyMeldlist();
	
	if (listLength < TILES_HAND_NUM - 3) { // avoid melding all tiles
	
		hand.meldlist[i] = hand.hand[index1];
		hand.meldlist[i + 1] = hand.hand[index2];
		hand.meldlist[i + 2] = newValue;
		
		hand.removeTile(index1);
		hand.removeTile(index2);
		
		hand.sortList(1);
	
	}
	
	return;
	
}

void player::pon(	const int &index1),
					const int &index2),
					const maajanTile &newValue) {
					
	int listLength = hand.getQtyMeldlist();
	
	if (listLength < TILES_HAND_NUM - 3) { // avoid melding all tiles
	
		hand.meldlist[i] = hand.hand[index1];
		hand.meldlist[i + 1] = hand.hand[index2];
		hand.meldlist[i + 2] = newValue;
		
		hand.removeTile(index1);
		hand.removeTile(index2);
		
		hand.sortList(1);
	
	}
	
	return;
	
}

void player::kan(	const int &index1),
					const int &index2),
					const int &index3),
					const maajanTile &newValue) {
					
	int listLength = hand.getQtyMeldlist();
	
	if (listLength < TILES_HAND_NUM - 4) { // avoid melding all tiles
	
		hand.meldlist[i] = hand.hand[index1];
		hand.meldlist[i + 1] = hand.hand[index2];
		hand.meldlist[i + 2] = hand.hand[index3];
		hand.meldlist[i + 3] = newValue;
		
		hand.removeTile(index1);
		hand.removeTile(index2);
		hand.removeTile(index3);
		
		hand.sortList(1);
	
	}
	
	return;
	
}


// ---------------------------------------------------------


void pass() {
	return;
}